package com.liustar.oplog.writer.consumer;

import com.alibaba.fastjson.JSON;
import com.liustar.oplog.model.OperationLog;
import com.liustar.oplog.writer.service.mapper.OpLogMapper;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Leo
 */
@Component
public class SimpleMqExecutor {
    private final Logger logger = Logger.getLogger(this.getClass().getName());

    @Autowired
    private OpLogMapper logMapper;

    /**
     * 写日志
     *
     * @param message
     * @return
     */
    public boolean doWriteOpLog(Message message, String logTable) {
        logger.info("消费消息：写日志");
        try {
            logger.log(Level.INFO, "写日志记录:" + logTable);
            OperationLog opLog = JSON.parseObject(message.getBody(), OperationLog.class);
            logMapper.insert(opLog, logTable);
            logger.info("消费成功：写日志");
            return true;
        } catch (Exception e) {
            logger.severe("消费失败：写日志出错！error:" + e.getMessage());
            return false;
        }
    }


}
